package com.itheima.tlais_web_management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlais_web_management.aop.Log;
import com.itheima.tlais_web_management.mapper.EmpMapper;
import com.itheima.tlais_web_management.pojo.Emp;
import com.itheima.tlais_web_management.pojo.PageBean;
import com.itheima.tlais_web_management.service.Empservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements Empservice {

    @Autowired
    private EmpMapper empMapper;

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//
//        //获取总记录数
//        Long count = empMapper.count();
//
//        //获取分页查询结果列表
//        Integer start = (page - 1 ) * pageSize;
//        List<Emp> list = empMapper.page(start ,pageSize);
//
//        //封装一个 pageBean 对象
//        PageBean pageBean = new PageBean(count,list);
//        return pageBean;
//    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {

        //设置分页参数
        Page<Object> startPage = PageHelper.startPage(page, pageSize);

        //执行查询
        List<Emp> list = empMapper.list(name,gender,begin,end);
        Page<Emp> emplist = (Page<Emp>) list;

        //封装一个 pageBean 对象
        PageBean pageBean = new PageBean(emplist.getTotal(),emplist.getResult());
        return pageBean;
    }

    @Log
    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.save(emp);
    }

    @Override
    public Emp getbyid(Integer id) {
         return empMapper.getbyid(id);
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.login(emp);
    }

    @Override
    public List<Emp> getbyjobid(Integer id) {
        return empMapper.getbyjobid(id);
    }
}
